/* 
* Copyright (c) Harry Pierson. 
*
* This source code is subject to terms and conditions of the Microsoft 
* Permissive License. A copy of the license can be found in the License.html 
* file at the root of this distribution. If you cannot locate the  Microsoft 
* Permissive License, please send an email to harry@devhawk.net. By using this 
* source code in any fashion, you are agreeing to be bound by the terms of the 
* Microsoft Permissive License.
* 
* You must not remove this notice, or any other, from this software.
*/

using System;
using System.Web;
using System.IO;
using System.CodeDom;
using System.Collections.Specialized;
using System.Collections;

namespace HawkWiki.WikiRenderer
{
    public abstract class WikiPage
    {
        //Overridden by dynamically generated WikiPage Subclasses
        public abstract void ProcessWiki(WikiWriter tw);

        #region WikiWord Prevalidation Code
        public class State
        {
            public bool PageExists;
            public string PageLink;
        }

        StringCollection wikiPages = new StringCollection();
        System.Collections.IDictionary wikiStates;

        public delegate IDictionary WikiWordValidateDelegate(StringCollection wikiPages);

        protected WikiWordValidateDelegate _wikiWordValidate = null;

        public WikiWordValidateDelegate WikiWordValidate
        {
            get { return _wikiWordValidate; }
            set { _wikiWordValidate = value; }
        }

        protected void AddWikiWord(string wikiword)
        {
            if (!wikiPages.Contains(wikiword))
                wikiPages.Add(wikiword);
        }

        protected void RunWikiWordValidation()
        {
            if (WikiWordValidate == null)
            {
                wikiStates = null;
                return;
            }

            wikiStates = WikiWordValidate(wikiPages);
        }
        #endregion

        #region WikiWord Exists and links Delegates

        protected void InitDelegates(WikiWriter tw)
        {
            tw.WikiWordExists = new WikiWriter.WikiWordExistsDelegate(this.CheckPageExists);
            tw.WikiWordLink = new WikiWriter.WikiWordLinkDelegate(this.RenderPageLink);
        }

        public bool CheckPageExists(string pageName)
        {
            if (wikiStates == null)
                return false;

            State pageState = (State)wikiStates[pageName];
            if (pageState == null)
                return false;

            return pageState.PageExists;
        }

        public string RenderPageLink(string pageName)
        {
            if (wikiStates == null)
                return null;

            State pageState = (State)wikiStates[pageName];
            if (pageState == null)
                return null;

            return pageState.PageLink;
        }
        #endregion
    }
}
